//
//  DSZSHDZVeiw.m
//  7.13苏宁框架
//
//  Created by app05 on 15/7/22.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "DSZSHDZVeiw.h"
#import "UserModel.h"
#import "UserDB.h"
#import "DSZDiZhiGLViewController.h"
@interface DSZSHDZVeiw ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property(nonatomic,strong)NSArray *provinces;
@property(nonatomic,strong)NSArray *cities;
@property(nonatomic,strong)NSArray *areas;
@property (weak, nonatomic) IBOutlet UIView *locationPickerView;

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *arrow;

@property (weak, nonatomic) IBOutlet UITableViewCell *cell;

@property(nonatomic,assign)BOOL flag;

@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *TEL;
@property (weak, nonatomic) IBOutlet UITextField *detailLoca;
@property (weak, nonatomic) IBOutlet UITextField *postNum;
- (IBAction)store;


@end
@implementation DSZSHDZVeiw


-(HZLocation *)locate
{
    if (_locate == nil) {
        _locate = [[HZLocation alloc] init];
    }
    
    return _locate;
}

+(instancetype)locationView
{
    return [[NSBundle mainBundle]loadNibNamed:@"DSZSHDZView" owner:nil options:nil][0];
}

-(void)awakeFromNib
{
    self.pickerView.delegate=self;
    self.pickerView.dataSource=self;
    //self.pickerView.frame=CGRectMake(0, 43, 320, 225);
    _provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]];
    _cities = [[_provinces objectAtIndex:0] objectForKey:@"cities"];
    
    self.locate.state = [[_provinces objectAtIndex:0] objectForKey:@"state"];
    self.locate.city = [[_cities objectAtIndex:0] objectForKey:@"city"];
    
    _areas = [[_cities objectAtIndex:0] objectForKey:@"areas"];
    if (_areas.count > 0) {
        self.locate.district = [_areas objectAtIndex:0];
    } else{
        self.locate.district = @"";
    }

    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    [self.cell addGestureRecognizer:tap];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 20;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
    
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return [_provinces count];
            break;
        case 1:
            return [_cities count];
            break;
        case 2:
            return [_areas count];
            break;
        default:
            return 0;
            break;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    switch (component) {
        case 0:
            return [[_provinces objectAtIndex:row] objectForKey:@"state"];
            break;
        case 1:
            return [[_cities objectAtIndex:row] objectForKey:@"city"];
            break;
        case 2:
            if ([_areas count] > 0) {
                return [_areas objectAtIndex:row];
                break;
            }
        default:
            return  @"";
            break;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    switch (component) {
        case 0:
            _cities = [[_provinces objectAtIndex:row] objectForKey:@"cities"];
            [self.pickerView selectRow:0 inComponent:1 animated:YES];
            [self.pickerView reloadComponent:1];
            
            _areas = [[_cities objectAtIndex:0] objectForKey:@"areas"];
            [self.pickerView selectRow:0 inComponent:2 animated:YES];
            [self.pickerView reloadComponent:2];
            
            self.locate.state = [[_provinces objectAtIndex:row] objectForKey:@"state"];
            self.locate.city = [[_cities objectAtIndex:0] objectForKey:@"city"];
            if ([_areas count] > 0) {
                self.locate.district = [_areas objectAtIndex:0];
            } else{
                self.locate.district = @"";
            }
            break;
        case 1:
            _areas = [[_cities objectAtIndex:row] objectForKey:@"areas"];
            [self.pickerView selectRow:0 inComponent:2 animated:YES];
            [self.pickerView reloadComponent:2];
            
            self.locate.city = [[_cities objectAtIndex:row] objectForKey:@"city"];
            if ([_areas count] > 0) {
                self.locate.district = [_areas objectAtIndex:0];
            } else{
                self.locate.district = @"";
            }
            break;
        case 2:
            if ([_areas count] > 0) {
                self.locate.district = [_areas objectAtIndex:row];
            } else{
                self.locate.district = @"";
            }
            break;
        default:
            break;
    }
    self.titleLabel.text = [NSString stringWithFormat:@"所在地区：%@ %@ %@", self.locate.state, self.locate.city, self.locate.district];
    self.arrow.alpha=0;
    
    
}

-(void)tap:(UITapGestureRecognizer *)tap
{
    if (!self.flag) {
                [UIView beginAnimations:nil context:NULL];
                [UIView setAnimationDuration:0.2];
        
                self.locationPickerView.frame=CGRectMake(0, 480-254, 320, 254);
        
                [UIView commitAnimations];
                self.flag=YES;
            }else
            {
                [UIView beginAnimations:nil context:NULL];
                [UIView setAnimationDuration:0.2];
        
                self.locationPickerView.frame=CGRectMake(0, 480, 320, 254);
                
                [UIView commitAnimations];
                self.flag=NO;
            }

}
- (IBAction)store {
    //userName ,telephone ,bigLoca ,littleLoca ,postNum
    NSString *str=[NSString stringWithFormat:@"%@ %@ %@",self.locate.state,self.locate.city,self.locate.district];
    UserModel *userModel=[[UserModel alloc]init];
    userModel.userName=self.name.text;
    userModel.telephone=self.TEL.text;
    userModel.bigLoca=str;
    userModel.littleLoca=self.detailLoca.text;
    userModel.postNum=self.postNum.text;
    
    [[UserDB shareInstance] addUser:userModel];
    
    if ([self.delegate respondsToSelector:@selector(backToLastController)]) {
        [self.delegate backToLastController];
    }
}
@end
